﻿namespace Kodefu
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class Node<T, TWeight> : INode<T, TWeight>
    {
        private List<IEdge<T, TWeight>> edges = new List<IEdge<T, TWeight>>();
        
        Func<INode<T, TWeight>, INode<T, TWeight>, TWeight> calculateWeight;

        public Func<INode<T, TWeight>, INode<T, TWeight>, TWeight> CalculateWeight
        {
            get
            {
                if (calculateWeight == null)
                {
                    calculateWeight = (n1, n2) => default(TWeight);
                }

                return calculateWeight;
            }
            set
            {
                calculateWeight = value;
            }
        }

        public IEnumerable<IEdge<T, TWeight>> Edges
        {
            get
            {
                return edges;
            }
            private set
            {
                edges = new List<IEdge<T, TWeight>>(value);
            }
        }

        public T Value { get; private set; }

        public Node(T value)
        {
            this.Value = value;
        }

        public void Add(INode<T, TWeight> node)
        {
            var edge = new Edge<T, TWeight>(this, node, CalculateWeight(this, node));
            edges.Add(edge);
        }
    }
}
